﻿using System;
using System.Linq;
using GatewayBizProcess.Db;
using Hjt212Parser;
using log4net;
using ToolsAndHjt212Parser;

namespace GatewayBizProcess.Facade
{
    public static class CommandUp
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(CommandUp));
        private static string _hyphen = "-";
        public static void Up3071(string content)
        {
            try
            {
                using (var ctx = new PlatformZrContext())
                {
                    var mn = Parser.GetStringByKey("MN", content);
                    if (string.IsNullOrEmpty(mn)) return;
                    var port = BaseInfoBuffer.GetInfo(mn);
                    if (port == null) return;
                    var realtime = ctx.state_realtime.FirstOrDefault(t => t.mn.Equals(mn));
                    if (realtime == null)
                    {
                        realtime = new state_realtime();
                        ctx.state_realtime.Add(realtime);
                    }
                    realtime.mn = mn;
                    realtime.datatime = Parser.GetDateTimeByKey("QN", content);
                    realtime.valve = Parser.GetStringByKey("Per", content);
                    realtime.bump = Parser.GetStringByKey("Pump", content);
                    realtime.gate = Parser.GetStringByKey("Gate", content);
                    realtime.is_online = true;
                    realtime.liquid_level = Parser.GetStringByKey("LiquidLevel", content);
                    realtime.power = _hyphen;
                    realtime.ps_base_id = port.ps_base_id;
                    realtime.ps_pi_id = port.id;
                    ctx.SaveChanges();
                    state_history history = new state_history
                    {
                        mn = mn,
                        datatime = Parser.GetDateTimeByKey("QN", content),
                        valve = Parser.GetStringByKey("Per", content),
                        bump = Parser.GetStringByKey("Pump", content),
                        gate = Parser.GetStringByKey("Gate", content),
                        is_online = true,
                        liquid_level = Parser.GetStringByKey("LiquidLevel", content),
                        power = _hyphen,
                        ps_base_id = port.ps_base_id,
                        ps_pi_id = port.id
                    };
                    ctx.state_history.Add(history);
                    ctx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        public static void Up3081(string content)
        {
            try
            {
                using (var ctx = new PlatformZrContext())
                {
                    var mn = Parser.GetStringByKey("MN", content);
                    if (string.IsNullOrEmpty(mn)) return;
                    var port = BaseInfoBuffer.GetInfo(mn);
                    if (port == null) return;
                    var realtime = ctx.state_realtime.FirstOrDefault(t => t.mn.Equals(mn));
                    if (realtime == null)
                    {
                        realtime = new state_realtime();
                        ctx.state_realtime.Add(realtime);
                    }
                    realtime.mn = mn;
                    realtime.datatime = Parser.GetDateTimeByKey("DataTime", content);
                    realtime.valve = _hyphen;
                    realtime.bump = _hyphen;
                    realtime.gate = _hyphen;
                    realtime.is_online = true;
                    realtime.liquid_level = _hyphen;
                    realtime.power = "OFF";
                    realtime.ps_base_id = port.ps_base_id;
                    realtime.ps_pi_id = port.id;
                    ctx.SaveChanges();
                    state_history history = new state_history
                    {
                        mn = mn,
                        datatime = Parser.GetDateTimeByKey("DataTime", content),
                        valve = _hyphen,
                        bump = _hyphen,
                        gate = _hyphen,
                        is_online = true,
                        liquid_level = _hyphen,
                        power = "OFF",
                        ps_base_id = port.ps_base_id,
                        ps_pi_id = port.id
                    };
                    ctx.state_history.Add(history);
                    ctx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

        }
        public static void Up3082(string content)
        {
            try
            {
                using (var ctx = new PlatformZrContext())
                {
                    var mn = Parser.GetStringByKey("MN", content);
                    if (string.IsNullOrEmpty(mn)) return;
                    var port = BaseInfoBuffer.GetInfo(mn);
                    if (port == null) return;
                    var realtime = ctx.state_realtime.FirstOrDefault(t => t.mn.Equals(mn));
                    if (realtime == null)
                    {
                        realtime = new state_realtime();
                        ctx.state_realtime.Add(realtime);
                    }
                    realtime.mn = mn;
                    realtime.datatime = Parser.GetDateTimeByKey("DataTime", content);
                    realtime.valve = _hyphen;
                    realtime.bump = _hyphen;
                    realtime.gate = _hyphen;
                    realtime.is_online = true;
                    realtime.liquid_level = _hyphen;
                    realtime.power = "ON";
                    realtime.ps_base_id = port.ps_base_id;
                    realtime.ps_pi_id = port.id;
                    ctx.SaveChanges();
                    state_history history = new state_history
                    {
                        mn = mn,
                        datatime = Parser.GetDateTimeByKey("DataTime", content),
                        valve = _hyphen,
                        bump = _hyphen,
                        gate = _hyphen,
                        is_online = true,
                        liquid_level = _hyphen,
                        power = "ON",
                        ps_base_id = port.ps_base_id,
                        ps_pi_id = port.id
                    };
                    ctx.state_history.Add(history);
                    ctx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

        }
        /// <summary>
        ///   请求应答告诉前端还要告诉平台
        /// </summary>
        /// <param name="content"></param>
        public static void Up9011(string content)
        {
            var qn = Parser.GetStringByKey("QN", content);
            if (string.IsNullOrEmpty(qn)) return;
            var qnRtn = Parser.GetStringByKey("QnRtn", content);
            if (qnRtn == null) return;
            try
            {
                using (var ctx = new GatewayTotalPilotContext())
                {
                    //是否为阀门控制、门禁控制、采样控制的请求应答指令（前端收发）
                    var sendCtrCmdItem = ctx.send_ctr_cmd.FirstOrDefault(t => t.qn.Equals(qn));
                    if (sendCtrCmdItem != null)
                    {
                        sendCtrCmdItem.is_sent = true;
                        sendCtrCmdItem.sent_time = DateTime.Now;
                        sendCtrCmdItem.rtn_9011 = qnRtn;
                    }
                    ctx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
            try
            {
                using (var ctx = new PlatformZrContext())
                {
                    //是否为阀门控制、门禁控制、采样控制的请求应答指令（平台）
                    var deviceCtrlItem = ctx.gateway_device_ctrl.FirstOrDefault(t => t.qn.Equals(qn));
                    if (deviceCtrlItem != null)
                    {
                        deviceCtrlItem.qn_rtn = qnRtn;
                        deviceCtrlItem.result = FlagTranslation.GetQnRtnMean(deviceCtrlItem.qn_rtn);
                        ctx.SaveChanges();
                    }

                    //是否为远程采样的请求应答指令（平台）
                    var sampleItem = ctx.gateway_sample_take.FirstOrDefault(t => t.qn.Equals(qn));
                    if (sampleItem != null)
                    {
                        sampleItem.qn_rtn = qnRtn;
                        sampleItem.result = FlagTranslation.GetQnRtnMean( sampleItem.qn_rtn);
                        var intByKey = Parser.GetIntByKey("BotID", content);
                        if (intByKey != null)
                            sampleItem.bottle_id = (int)intByKey;
                        ctx.SaveChanges();
                    }
                    //todo:开卡5012响应没做
                    //todo:设定月允许排放量3093响应没做
                    //todo:设置许可证污染物信息3094响应没做
                    //todo:远程控制门禁3097响应没做

                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        /// <summary>
        ///   操作结果应答告诉前端还要告诉平台
        /// </summary>
        /// <param name="content"></param>
        public static void Up9012(string content)
        {
            var qn = Parser.GetStringByKey("QN", content);
            if (string.IsNullOrEmpty(qn)) return;
            var exeRtn = Parser.GetStringByKey("ExeRtn", content);
            try
            {
                using (var ctx = new GatewayTotalPilotContext())
                {
                    //是否为阀门控制、门禁控制、采样控制的请求应答指令（前端收发）
                    var sendCtrCmdItem = ctx.send_ctr_cmd.FirstOrDefault(t => t.qn.Equals(qn));
                    if (sendCtrCmdItem != null)
                    {
                        sendCtrCmdItem.is_sent = true;
                        sendCtrCmdItem.sent_time = DateTime.Now;
                        sendCtrCmdItem.rtn_9012 = exeRtn;
                    }
                    ctx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
            try
            {
                using (var ctx = new PlatformZrContext())
                {
                    //是否为阀门控制、门禁控制的请求应答指令（平台）
                    var deviceCtrItem = ctx.gateway_device_ctrl.FirstOrDefault(t => t.qn.Equals(qn));
                    if (deviceCtrItem != null)
                    {
                        deviceCtrItem.exe_rtn = exeRtn;
                        deviceCtrItem.result = FlagTranslation.GetExeRtnMean(deviceCtrItem.exe_rtn);
                        ctx.SaveChanges();
                    }

                    //是否为采样控制的请求应答指令（平台）
                    var sampleItem = ctx.gateway_sample_take.FirstOrDefault(t => t.qn.Equals(qn));
                    if (sampleItem != null)
                    {
                        sampleItem.exe_rtn = exeRtn;
                        sampleItem.result = FlagTranslation.GetExeRtnMean(sampleItem.exe_rtn);
                        ctx.SaveChanges();
                    }

                    //todo:开卡5012响应没做
                    //todo:设定月允许排放量3093响应没做
                    //todo:设置许可证污染物信息3094响应没做
                    //todo:远程控制门禁3097响应没做
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        /// <summary>
        /// 数据应答告诉前端就好
        /// </summary>
        /// <param name="content"></param>
        public static void Up9014(string content)
        {
            try
            {
                using (var ctx = new GatewayTotalPilotContext())
                {
                    var qn = Parser.GetStringByKey("QN", content);
                    //是否为小时数据下发的数据应答指令
                    var sendDataItem = ctx.send_data.FirstOrDefault(t => t.qn.Equals(qn));
                    if (sendDataItem != null)
                    {
                        sendDataItem.is_sent = true;
                        sendDataItem.sent_time = DateTime.Now;
                    }
                    ctx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        public static void Up3052(string content)
        {
            try
            {
                var qn = Parser.GetStringByKey("QN", content);
                var cn = "3052";
                var dataTime = Parser.GetDateTimeByKey("DataTime", content);
                var per = Parser.GetDoubleByKey("per", content);
                var mn = Parser.GetStringByKey("MN", content);
                using (var pCtx = new PlatformZrContext())
                {
                    //是否为阀门控制、门禁控制、采样控制的请求应答指令（平台）
                    var sendCtrCmdItem = new gateway_device_ctrl
                    {
                        mn = mn,
                        execute_time = dataTime,
                        ctrl_code = cn,
                        param = per,
                        qn = qn,
                        qn_rtn = "-",
                        exe_rtn = "-"
                    };
                    pCtx.gateway_device_ctrl.Add(sendCtrCmdItem);
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        public static void Up3097(string content)
        {
            try
            {
                var qn = Parser.GetStringByKey("QN", content);
                var cn = "3098";
                var dataTime = Parser.GetDateTimeByKey("DataTime", content);
                var mn = Parser.GetStringByKey("MN", content);
                var cardNo = Parser.GetStringByKey("CardNo", content);
                var cardType = Parser.GetStringByKey("CardType", content);
                var gate = Parser.GetStringByKey("Gate", content);

                using (var pCtx = new PlatformZrContext())
                {
                    //是否为阀门控制、门禁控制、采样控制的请求应答指令（平台）
                    var sendCtrCmdItem = new gateway_device_ctrl
                    {
                        mn = mn,
                        execute_time = dataTime,
                        ctrl_code = cn,
                        qn = qn,
                        qn_rtn = "-",
                        exe_rtn = "-",
                        card_num = cardNo,
                        card_type = cardType,
                        param = gate.Equals("1") ? 1 : 0
                    };
                    pCtx.gateway_device_ctrl.Add(sendCtrCmdItem);
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        public static void Up4012(string content)
        {
            try
            {
                var qn = Parser.GetStringByKey("QN", content);
                var cn = "4012";

                var mn = Parser.GetStringByKey("MN", content);
                var codes = Parser.GetPollutantCodes(content);
                using (var pCtx = new PlatformZrContext())
                {
                    foreach (var code in codes)
                    {
                        var dataTime = Parser.GetDateTimeByKey(code + "-Time", content);
                        var param = Parser.GetDoubleByKey(code + "-Quantity", content);
                        var index = int.Parse(content.Replace("Bottle", ""));
                        //是否为阀门控制、门禁控制、采样控制的请求应答指令（平台）
                        var sendCtrCmdItem = new gateway_sample_take
                        {
                            mn = mn,
                            execute_time = dataTime,
                            ctrl_code = cn,
                            qn = qn,
                            qn_rtn = "-",
                            exe_rtn = "-",
                            param = param,
                            bottle_id = index
                        };
                        pCtx.gateway_sample_take.Add(sendCtrCmdItem);
                    }
                    pCtx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
    }
}
